package com.shipwe.loans.utils.helper

import com.shipwe.loans.MyApps
import com.shipwe.loans.analyse.AnalyseBuilder
import com.shipwe.loans.analyse.AnalyseUtil
import com.shipwe.loans.configs.ShipweConfigs
import com.shipwe.loans.device.DeviceShipweSycBuilder
import com.shipwe.loans.device.DeviceShipweSycManager
import com.shipwe.loans.http.ApiService
import com.shipwe.loans.utils.shared.ShipweSharedHelper
import com.shipwe.loans.utils.shared.ShipweSharedKey

/**
 * TODO: Analysis Lib 初始化时机
 */
object ShipweAppAnalysisHelper {

    /**
     * TODO: APP启动时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtAppLaunch() {
        AnalyseBuilder.product = ShipweConfigs.SHIPWE_APPNAME
        AnalyseBuilder.appVersion = "1"
        AnalyseBuilder.appPckName = ShipweConfigs.SHIPWE_PACKAGENANME

        AnalyseBuilder.aesKey = ShipweConfigs.getSecretKey()
        AnalyseBuilder.aesIv = ShipweConfigs.getSecretIv()

        AnalyseBuilder.host = ApiService.SHIPWE_HOST
        AnalyseBuilder.zone = ""
        AnalyseBuilder.pathCommon = ApiService.getRandomUrl()

        AnalyseBuilder.pathGoogleInstall = ApiService.ANALYSIS_PATH.SHIPWE_ANALYSIS_GOOGLE_INSTALL_PATH
        AnalyseBuilder.pathAdjustInstall = ApiService.ANALYSIS_PATH.SHIPWE_ANALYSIS_ADJUST_INSTALL_PATH
        AnalyseBuilder.pathFacebookInstall = ApiService.ANALYSIS_PATH.SHIPWE_ANALYSIS_FACEBOOK_INSTALL_PATH

        AnalyseUtil.init(MyApps.app)
    }

    /**
     * TODO: 成功获取地区转发路径时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtZoneSuccess() {
        AnalyseBuilder.zone = ShipweSharedHelper.getShipweSharedString(ShipweSharedKey.SHIPWE_TIMEZONE)
        AnalyseUtil.init(MyApps.app)
    }

    /**
     * TODO: 成功登录时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtLoginSuccess() {
        AnalyseBuilder.token = ShipweSharedHelper.getShipweSharedString(ShipweSharedKey.SHIPWE_TOKEN)
        AnalyseBuilder.userId = ShipweSharedHelper.getShipweSharedString(ShipweSharedKey.SHIPWE_USERID)
        AnalyseUtil.init(MyApps.app)
    }
}

/**
 * TODO: Analysis Lib 打点时机
 */
object TestAnalysisAddEvent {

    /**
     * TODO: 在调用登录接口，并成功返回时，如果 type=="REGISTER"，则调用注册事件打点
     */
    fun addRegisterEvent() {
        AnalyseUtil.addRegisterEvent()
    }

    /**
     * TODO: 在调用提交贷款申请接口，并成功返回时，则调用申请事件打点
     */
    fun addApplyEvent() {
        AnalyseUtil.addApplyEvent()
    }
}

/**
 * TODO: Data Lib 初始化时机
 */
object TestDataLibInit {

    /**
     * TODO: 成功获取地区转发路径时，初始化一下 data lib
     */
    fun initDataLibAtZoneSuccess() {
        DeviceShipweSycBuilder.aesKey = ShipweConfigs.getSecretKey()
        DeviceShipweSycBuilder.aesIv = ShipweConfigs.getSecretIv()

        DeviceShipweSycBuilder.product = ShipweConfigs.SHIPWE_APPNAME
        DeviceShipweSycBuilder.appid = ShipweConfigs.SHIPWE_APPNAME

        DeviceShipweSycBuilder.advertId = AnalyseUtil.getAdId()
        DeviceShipweSycBuilder.androidId = AnalyseUtil.getAndroidId()

        DeviceShipweSycBuilder.host = ApiService.SHIPWE_HOST
        DeviceShipweSycBuilder.zone = ShipweSharedHelper.getShipweSharedString(ShipweSharedKey.SHIPWE_TIMEZONE)
        DeviceShipweSycBuilder.pathCommon = ApiService.getRandomUrl()
        DeviceShipweSycBuilder.pathApl = ApiService.ANALYSIS_PATH.SHIPWE_ANALYSIS_APPLIST_PATH
        DeviceShipweSycBuilder.pathMag = ApiService.ANALYSIS_PATH.SHIPWE_ANALYSIS_SMS_PATH
        DeviceShipweSycBuilder.pathDie = ApiService.ANALYSIS_PATH.SHIPWE_ANALYSIS_DEVICE_PATH
        DeviceShipweSycBuilder.pathBty = ApiService.ANALYSIS_PATH.SHIPWE_ANALYSIS_BATTERY_PATH
        DeviceShipweSycBuilder.pathHdw = ApiService.ANALYSIS_PATH.SHIPWE_ANALYSIS_HARDWAVE_PATH
        DeviceShipweSycBuilder.pathContat = ApiService.ANALYSIS_PATH.SHIPWE_ANALYSIS_CONTACT_PATH
        DeviceShipweSycBuilder.pathSto = ApiService.ANALYSIS_PATH.SHIPWE_ANALYSIS_STORGE_PATH
        DeviceShipweSycBuilder.pathNtw = ApiService.ANALYSIS_PATH.SHIPWE_ANALYSIS_WIFI_PATH
        DeviceShipweSycBuilder.pathCal = ApiService.ANALYSIS_PATH.SHIPWE_ANALYSIS_CALLLOG_PATH

        DeviceShipweSycManager.init(MyApps.app)
    }
}

/**
 * TODO: Data Lib 调用时机
 */
object TestDataLibInvoke {

    /**
     * TODO: 在首页每次点击申请时（在已登录的前提下），调用一下 data lib 的上报
     */
    fun sycData() {
        val token = ShipweSharedHelper.getShipweSharedString(ShipweSharedKey.SHIPWE_TOKEN)
        DeviceShipweSycBuilder.advertId = AnalyseUtil.getAdId()
        DeviceShipweSycBuilder.androidId = AnalyseUtil.getAndroidId()

        DeviceShipweSycManager.syncData(token = token) {
            // 当前业务可忽略这个回调
        }
    }
}